package com.hopedove.workshop.ui

import android.graphics.Color
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RadioButton
import androidx.appcompat.content.res.AppCompatResources
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.LinearLayoutManager
import com.hopedove.base.BaseActivity
import com.hopedove.workshop.R
import com.hopedove.workshop.api.MaterialItem
import com.hopedove.workshop.api.RunningSituationType
import com.hopedove.workshop.data.pojo.ObservableRecordItem
import com.hopedove.workshop.databinding.WorkshopRunningSituationRecordBinding
import com.hopedove.workshop.ui.adapter.RunningSituationRecordAdapter
import com.hopedove.workshop.ui.dialog.ListSelectionDialog
import com.hopedove.workshop.utils.dip
import com.hopedove.workshop.viewmodel.WorkshopRunningSituationRecordViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 *  运转情况录入界面
 *
 */
class RunningSituationRecordActivity : BaseActivity() {

    private lateinit var binding: WorkshopRunningSituationRecordBinding
    private val viewModel: WorkshopRunningSituationRecordViewModel by viewModel()
    private val self = this

    private lateinit var materialsDialog: ListSelectionDialog
    private val recordAdapter = RunningSituationRecordAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.workshop_running_situation_record)
        binding.vm = viewModel

        setupRecordListView()
        setViewModelListeners()
        viewModel.load()
    }

    private fun setupRecordListView() {
        binding.rlList.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            adapter = recordAdapter
        }
    }

    private fun setViewModelListeners() {
        observeLoading(viewModel.loading)
        observeNavigation(viewModel.navigation)
        observeToastText(viewModel.toastText)
        observeMaterials(viewModel.materials)
        observeRecordList(viewModel.recordItems)
        setTypeButtons(viewModel.types)
    }

    private fun observeRecordList(recordItem: LiveData<List<ObservableRecordItem>>) {
        recordItem.observe(this) {
            recordAdapter.setData(it)
        }
    }

    private fun observeMaterials(materials: LiveData<List<MaterialItem>>) {
        materials.observe(this) {
            if (!::materialsDialog.isInitialized) {
                materialsDialog = ListSelectionDialog()
                materialsDialog.mustSelectedToConfirm = true
            }

            if (materialsDialog.dialog?.isShowing == true) {
                materialsDialog.dialog?.dismiss()
            }

            materialsDialog.setData(it.map { item -> ListSelectionDialog.ListItem(text = item.name) })
            materialsDialog.getSelectedItem = { indexes ->
                viewModel.onMaterialListItemSelected(indexes)
            }

            materialsDialog.show(supportFragmentManager, "material list")
        }
    }

    private fun setTypeButtons(types: LiveData<List<RunningSituationType>>) {
        types.observe(this, { items ->
            binding.typeSelectionContainer.removeAllViews()

            items.forEachIndexed { index, type ->
                val view = type.let {
                    RadioButton(this).apply {
                        id = View.generateViewId()
                        buttonDrawable = null
                        text = it.name
                        setTextColor(Color.WHITE)
                        setTextSize(
                            TypedValue.COMPLEX_UNIT_PX,
                            resources.getDimension(R.dimen.radio_button_text_size)
                        )
                        background = AppCompatResources.getDrawable(
                            self,
                            R.drawable.workshop_rounded_background
                        )
                        backgroundTintList = AppCompatResources.getColorStateList(
                            self,
                            R.color.workshop_check_state_selector
                        )
                        tag = index
                    }
                }

                val layoutParams = LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )

                layoutParams.marginStart = resources.dip(20f)
                layoutParams.marginEnd = resources.dip(20f)

                binding.typeSelectionContainer.addView(view, layoutParams)
            }
        })
    }

}